import { ApiResponse, Profile, User } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'

export const getUser = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<User>>('/user')
    dispatch({
      type: 'profile/getUser',
      payload: res.data.data,
    })
  }
}
export const getUserProfile = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<Profile>>('/user/profile')
    dispatch({
      type: 'profile/getUserProfile',
      payload: res.data.data,
    })
  }
}
export const updateUserProfile = (data: Partial<Profile>): RootThunkAction => {
  return async (dispatch) => {
    await request.patch('/user/profile', data)
    dispatch(getUserProfile())
  }
}
export const updataPhoto = (fd: FormData): RootThunkAction => {
  return async (dispatch) => {
    await request.patch('/user/photo', fd)
    dispatch(getUserProfile())
  }
}
